<?php

/*
 * Method for validating posted values.
 * @access public
 * @retrun bool Returns true if the validation was successful, otherwise sends invalid response.
 */
function valid_add_country()
{
    $ci = &get_instance();
    $cms = $ci->cms;

    $ci->form_validation
            ->set_rules('code',             'code',             "trim|required|is_unique[{$cms['db_prefix']}countries.code]|max_length[3]|xss_clean")
            ->set_rules('name',             'name',             'trim|required|max_length[52]|xss_clean')
            ->set_rules('continent',        'continent',        'trim|required|max_length[1000]|xss_clean')
            ->set_rules('region',           'region',           'trim|required|max_length[26]|xss_clean')
            ->set_rules('surface_area',     'surface_area',     'trim|required|max_length[10]|xss_clean')
            ->set_rules('indep_year',       'indep_year',       'trim|required|max_length[6]|xss_clean')
            ->set_rules('population',       'population',       'trim|required|max_length[11]|xss_clean')
            ->set_rules('life_expectancy',  'life_expectancy',  'trim|required|max_length[10]|xss_clean')
            ->set_rules('gnp',              'gnp',              'trim|required|max_length[10]|xss_clean')
            ->set_rules('gnp_old',          'gnp_old',          'trim|required|max_length[10]|xss_clean')
            ->set_rules('local_name',       'local_name',       'trim|required|max_length[45]|xss_clean')
            ->set_rules('government_form',  'government_form',  'trim|required|max_length[45]|xss_clean')
            ->set_rules('head_of_state',    'head_of_state',    'trim|required|max_length[60]|xss_clean')
            ->set_rules('capital',          'capital',          'trim|required|max_length[11]|xss_clean')
            ->set_rules('code2',            'code2',            'trim|required|max_length[2]|xss_clean');
    
    $ci->form_validation
            ->set_message('is_unique', 'This country code is already existing');
    
    return $ci->form_validation->run();
    
//    if ($ci->form_validation->run() == FALSE)
//    {
//
//        switch( $action )
//        {
//            case 'add' :
//                view_module_form('Add Module', 'add_failed');
//                break;
//            case 'edit' :
//                $module = $ci->module_model->get_by_id( set_value('id') );
//                view_module_form('Edit Module', 'edit_failed', $module);
//                break;
//            case 'api_add' :
//                echo 'api add';
//                break;
//            case 'api_edit' :
//                echo 'api edit';
//                break;
//            default :
//                return FALSE;
//                break;
//        }
//    } else
//    {
//        return TRUE;
//    }
}


/*
 * Method for validating posted values.
 * @access public
 * @retrun bool Returns true if the validation was successful, otherwise sends invalid response.
 */
function valid_edit_country()
{
    $ci = &get_instance();
    
    $ci->input->post('id')                  ? $ci->form_validation->set_rules('id', 'ID', 'trim|required|numeric|max_length[10]|xss_clean') : '';
    $ci->input->post('code')                ? $ci->form_validation->set_rules('code', 'code', "trim|required|max_length[3]|xss_clean") : '';
    $ci->input->post('name')                ? $ci->form_validation->set_rules('name', 'name', 'trim|required|max_length[52]|xss_clean') : '';
    $ci->input->post('continent')           ? $ci->form_validation->set_rules('continent', 'continent', 'trim|required|max_length[1000]|xss_clean') : '';
    $ci->input->post('region')              ? $ci->form_validation->set_rules('region', 'region', 'trim|required|max_length[26]|xss_clean') : '';
    $ci->input->post('surface_area')        ? $ci->form_validation->set_rules('surface_area', 'surface area', 'trim|required|max_length[10]|xss_clean') : '';
    $ci->input->post('indep_year')          ? $ci->form_validation->set_rules('indep_year', 'indep year', 'trim|required|max_length[6]|xss_clean') : '';
    $ci->input->post('population')          ? $ci->form_validation->set_rules('population', 'population', 'trim|required|max_length[11]|xss_clean') : '';
    $ci->input->post('life_expectancy')     ? $ci->form_validation->set_rules('life_expectancy', 'life expectancy', 'trim|required|max_length[10]|xss_clean') : '';
    $ci->input->post('gnp')                 ? $ci->form_validation->set_rules('gnp', 'gnp', 'trim|required|max_length[10]|xss_clean') : '';
    $ci->input->post('gnp_old')             ? $ci->form_validation->set_rules('gnp_old', 'gnp old', 'trim|required|max_length[10]|xss_clean') : '';
    $ci->input->post('local_name')          ? $ci->form_validation->set_rules('local_name', 'local name', 'trim|required|max_length[45]|xss_clean') : '';
    $ci->input->post('government_form')     ? $ci->form_validation->set_rules('government_form', 'government form', 'trim|required|max_length[45]|xss_clean') : '';
    $ci->input->post('head_of_state')       ? $ci->form_validation->set_rules('head_of_state', 'head of state', 'trim|required|max_length[60]|xss_clean') : '';
    $ci->input->post('capital')             ? $ci->form_validation->set_rules('capital', 'capital', 'trim|required|max_length[11]|xss_clean') : '';
    $ci->input->post('code2')               ? $ci->form_validation->set_rules('code2', 'code 2', 'trim|required|max_length[2]|xss_clean') : '';
    
    $ci->form_validation
            ->set_message('is_unique', 'This country code is already existing');
    
    return $ci->form_validation->run();
}

/*
 * Method for validating posted values.
 * @access public
 * @retrun bool Returns true if the validation was successful, otherwise sends invalid response.
 */
function valid_delete_country()
{
    $ci = &get_instance();
    $ci->input->post('id')                  ? $ci->form_validation->set_rules('id', 'ID', 'trim|required|numeric|max_length[10]|xss_clean') : '';
    return $ci->form_validation->run();
}

function add_country()
{
    $ci = &get_instance();
    
    $country = array();
    $country['code']                = clean_text( set_value('code') );
    $country['name']                = clean_text( set_value('name') );
    $country['continent']           = clean_text( set_value('continent') );
    $country['region']              = clean_text( set_value('region') );
    $country['surface_area']        = set_value('surface_area');
    $country['indep_year']          = set_value('indep_year');
    $country['population']          = set_value('population');
    $country['life_expectancy']     = set_value('life_expectancy');
    $country['gnp']                 = set_value('gnp');
    $country['gnp_old']             = set_value('gnp_old');
    $country['local_name']          = clean_text( set_value('local_name') );
    $country['government_form']     = clean_text( set_value('government_form') );
    $country['head_of_state']       = clean_text( set_value('head_of_state') );
    $country['capital']             = clean_text( set_value('capital') );
    $country['code2']              = clean_text( set_value('code2') );
    
    $country_id = $ci->country_model->insert($country);
    $country['id'] = $country_id;
    return $country;
}

function edit_country( $id = NULL )
{
    $ci = &get_instance();
    
    $country = array();
    $ci->input->post('id')                  ? $country['id']                = set_value('id') : '';
    $ci->input->post('code')                ? $country['code']              = clean_text( set_value('code') ) : '';
    $ci->input->post('name')                ? $country['name']              = clean_text( set_value('name') ) : '';
    $ci->input->post('continent')           ? $country['continent']         = clean_text( set_value('continent') ) : '';
    $ci->input->post('region')              ? $country['region']            = clean_text( set_value('region') ) : '';
    $ci->input->post('surface_area')        ? $country['surface_area']      = set_value('surface_area') : '';
    $ci->input->post('indep_year')          ? $country['indep_year']        = set_value('indep_year') : '';
    $ci->input->post('population')          ? $country['population']        = set_value('population') : '';
    $ci->input->post('life_expectancy')     ? $country['life_expectancy']   = set_value('life_expectancy') : '';
    $ci->input->post('gnp')                 ? $country['gnp']               = set_value('gnp') : '';
    $ci->input->post('gnp_old')             ? $country['gnp_old']           = set_value('gnp_old') : '';
    $ci->input->post('local_name')          ? $country['local_name']        = clean_text( set_value('local_name') ) : '';
    $ci->input->post('government_form')     ? $country['government_form']   = clean_text( set_value('government_form') ) : '';
    $ci->input->post('head_of_state')       ? $country['head_of_state']     = clean_text( set_value('head_of_state') ) : '';
    $ci->input->post('capital')             ? $country['capital']           = clean_text( set_value('capital') ) : '';
    $ci->input->post('code2')              ? $country['code2']            = clean_text( set_value('code2') ) : '';

    $country_id = isset( $country['id'] ) ? $country['id'] : $id;
    $edited_rows = $ci->country_model->update_by_id( $country_id, $country );
    $response_country = $edited_rows >= 0  ? $ci->country_model->get_by_id($country_id) : NULL;
    return $response_country;
}

function delete_country( $id = NULL )
{
    $ci = &get_instance();
    
    $country = array();
    $ci->input->post('id') ? $country['id'] = set_value('id') : '';
    
    $country_id = isset( $country['id'] ) ? $country['id'] : $id;
    $deleted = $ci->country_model->delete_by_id( $country_id );
    return $deleted;
}

/**
 * Formats an array of records to valid response.
 * @access public
 * @param array $countries Array of countries
 */
function response_countries($countries)
{
    $ci = &get_instance();
    
    $params = $ci->input->post();
    $case = isset( $params['case'] ) ? $params['case'] : 'snake';
    
    $response_countries = array();
    foreach($countries as $country)
    {
        $response_country                   = array();
        isset($country['id'])               ? $response_country[changeCase('id', $case)]               = clean( $country['id'] ) : '';
        isset($country['code'])             ? $response_country[changeCase('code', $case)]             = clean( $country['code'] ) : '';
        isset($country['name'])             ? $response_country[changeCase('name', $case)]             = clean( $country['name'] ) : '';
        isset($country['continent'])        ? $response_country[changeCase('continent', $case)]        = clean( $country['continent'] ) : '';
        isset($country['region'])           ? $response_country[changeCase('region', $case)]           = clean( $country['region'] ) : '';
        isset($country['surface_area'])     ? $response_country[changeCase('surface_area', $case)]     = clean( $country['surface_area'] ) : '';
        isset($country['indep_year'])       ? $response_country[changeCase('indep_year', $case)]       = clean( $country['indep_year'] ) : '';
        isset($country['population'])       ? $response_country[changeCase('population', $case)]       = clean( $country['population'] ) : '';
        isset($country['life_expectancy'])  ? $response_country[changeCase('life_expectancy', $case)]  = clean( $country['life_expectancy'] ) : '';
        isset($country['gnp'])              ? $response_country[changeCase('gnp', $case)]              = clean( $country['gnp'] ) : '';
        isset($country['gnp_old'])          ? $response_country[changeCase('gnp_old', $case)]          = clean( $country['gnp_old'] ) : '';
        isset($country['local_name'])       ? $response_country[changeCase('local_name', $case)]       = clean( $country['local_name'] ) : '';
        isset($country['government_form'])  ? $response_country[changeCase('government_form', $case)]  = clean( $country['government_form'] ) : '';
        isset($country['head_of_state'])    ? $response_country[changeCase('head_of_state', $case)]    = clean( $country['head_of_state'] ) : '';
        isset($country['capital'])          ? $response_country[changeCase('capital', $case)]          = clean( $country['capital'] ) : '';
        isset($country['code2'])            ? $response_country[changeCase('code2', $case)]           = clean( $country['code2'] ) : '';

        array_push($response_countries, $response_country);
    }
    
    // we format our response
    if( count($response_countries) > 0 ) // check if not empty
    {
        format_response('success', 200, 'Response Countries.', NULL, $response_countries);
    }
    else
    {
        format_response('success', 200, 'No Data Available', NULL, 'No Data Available');
    }
}

/* End of file country_helper.php */
/* Location: ./application/modules/api/helpers/country_helper.php */